//AlertWindow button image default paths
var kALERT_WINDOW_BACKGROUND  = "./Content/images/gamelayer/controllayer/main-menu-background.png"

// Ok buttons
var kALERT_WINDOW_OK_NORMAL = "./Content/images/gamelayer/generic-button-normal.png"
var kALERT_WINDOW_OK_SELECTED = "./Content/images/gamelayer/generic-button-pressed.png"

// Cancel buttons
var kALERT_WINDOW_CANCEL_NORMAL ="./Content/images/gamelayer/generic-button-normal.png"
var kALERT_WINDOW_CANCEL_SELECTED ="./Content/images/gamelayer/generic-button-pressed.png"

exports.MMEditTextAlert = UI.View.subclass({
	
	classname: 'MMEditTextAlert',
	
	initialize: function($super,initWithTitle,delegate,characterLimit) 
	{
		$super();

		this._tagValue					= null;
		
		this._title 					= null;
		this._delegate 					= null;
		this._characterLimit 			= 15;
		
		this._okButton					= null;
		this._cancelButton 				= null;
		this._titleLabel	    		= null;
		this._backgroundImage 			= null;
		this._type						= null;
		
		this._backgroundImagePath		= null;
		this._okImageNormalPath 		= null;
		this._okImageSelectedPath 		= null;
		this._cancelImageNormalPath		= null;
		this._cancelImageSelectedPath	= null;
		this._editText					= null;
		
		
		// initializing data members with parameters 
		if (initWithTitle) 
		{
			this._title = initWithTitle;
		}
		if (characterLimit)
		{
			this._characterLimit = characterLimit;
		}
		this._delegate = delegate;

		// setting images default path
		this._backgroundImagePath		=kALERT_WINDOW_BACKGROUND;
		this._okImageNormalPath			=kALERT_WINDOW_OK_NORMAL;
		this._okImageSelectedPath		=kALERT_WINDOW_OK_SELECTED;
		this._cancelImageNormalPath	 	=kALERT_WINDOW_CANCEL_NORMAL;
		this._cancelImageSelectedPath	=kALERT_WINDOW_CANCEL_SELECTED;
		
		// setting frame of this view
		var w = Core.Capabilities.getScreenWidth();
		var h = Core.Capabilities.getScreenHeight();
		
		this.setFrame([0,0,w,h]);
		
		this.setOnLoad(this.loadView.bind(this));
	},
	
	loadView: function()
	 {
	 	var frame=null;
	 	//var x = ((this.h - 356 )/2) - (356/2.4);
		//var y = ((this.w - 229 )/2) - (229/2.75);
        
        var x = 60;
        var y = 62;
										 
		this._backgroundImage = new UI.Image();
		this._backgroundImage.setImage(this._backgroundImagePath);
		this._backgroundImage.setImageFit(UI.ViewGeometry.FitModes.Stretch);
		this._backgroundImage.setFrame([this.getAdjustedX(x),this.getAdjustedY(y),this.adjustWidthWithDevice(200),this.adjustHeightWithDevice(200)]);
		this.addChild(this._backgroundImage);
		
			
		frame=[this.getAdjustedX(x+35),this.getAdjustedY(y+145),this.adjustWidthWithDevice(60),this.adjustHeightWithDevice(40)];
		this._okButton = UIFactory.getGenericButton(frame, this._okImageNormalPath, this._okImageSelectedPath,  Core.Localization.getString("OK"), "000000", TCUtil.getAdjustedFontSize(120), this);
		this._okButton.setOnClick(this.clickedButtonOk.bind(this));
		this.addChild(this._okButton);
		
		frame=[this.getAdjustedX(x+105),this.getAdjustedY(y+145),this.adjustWidthWithDevice(60),this.adjustHeightWithDevice(40)];
		this._cancelButton = UIFactory.getGenericButton(frame, this._cancelImageNormalPath, this._cancelImageSelectedPath,  Core.Localization.getString("CANCEL"), "000000", TCUtil.getAdjustedFontSize(100), this);
		this._cancelButton.setOnClick(this.clickedButtonCancel.bind(this));
		this.addChild(this._cancelButton);
	
		this._titleLabel = new UI.Label();
		this._titleLabel.setFrame([this.getAdjustedX(x),this.getAdjustedY(y+15),this.adjustWidthWithDevice(200),this.adjustHeightWithDevice(30)]);
		this._titleLabel.setTextSize(this.getAdjustedFontSize(100));
		this._titleLabel.setTextGravity(UI.ViewGeometry.Gravity.Center);
		this._titleLabel.setText(this._title);	
		this.addChild(this._titleLabel);
		
		
		this._editText = new UI.EditText() ;
		this._editText.setFrame([this.getAdjustedX(83),this.getAdjustedY(120),this.getAdjustedX(150),this.getAdjustedY(50)]) ;
		//this._editText.setBackgroundColor("FFFFFF") ;
		this._editText.setBackgroundColor("8E97AE") ;////142,151,174
		this._editText.setInputType(UI.EditText.InputTypes.Email);
		
		this._editText.setOnChange(this.updateEditTextField.bind(this)) ;
		
		this.addChild(this._editText) ;
	},
	
	updateEditTextField: function ()
	{
//		console.log("zaki:::: mallname is "+this._editText.getText());
//		console.log("zaki:::: mall[0] is "+this._editText.getText()[0]);
//		console.log("zaki:::: mall.length is "+this._editText.getText().length);
		
		if(this._editText.getText().length > this._characterLimit)
		{
			this._editText.setText(this._editText.getText().slice(0, this._characterLimit));
		}
	},
	
	show: function()
	{
		NGWindow.document.addChild(this);
	},
	
	// on click call backs
	clickedButtonOk: function()
	{
		this.clickedButtonAtIndex(0)		
	 	return true;
	},
	
	clickedButtonCancel: function()
	{
		this.clickedButtonAtIndex(1)		
		return true;	
	},
	
	
	clickedButtonAtIndex: function(buttonIndex)
	{
		
		if( this._delegate)		
		{
			this._delegate.clickedButtonAtIndex(buttonIndex,this);
		}
		
	},
	
	//getter
	getTagValue: function()
	{
		return this._tagValue;
	},
	
	getText: function ()
	{
		return this._editText.getText();
	},
	
	
	//setters
	
	setTagValue: function(value)
	{
		this._tagValue = value;
	},

	setBackgroundImagePath: function(path)
	{
		this._backgroundImagePath =path;
	},
	
	setOkImageNormalPath: function(path)
	{
		this._okImageNormalPath =path;
	},
	
	setOkImageSelectedPath: function(path)
	{
		this._okImageSelectedPath =path;
	},
	
	setCancelImageNormalPath: function(path)
	{
		this._cancelImageNormalPath =path;
	},
	
	setCancelImageSelectedPath: function(path)
	{
		this._cancelImageSelectedPath =path;
	},
		
	////////////////////////////////////////////////////////////
	
	getAdjustedX: function(x) {
   		
   		var width = 0 , newWidth = 0 ;
   		width = Core.Capabilities.getScreenWidth();
   		newWidth = (width / 320) * x;
   		
       	return newWidth;
   		
   },
    
   getAdjustedY: function(y) 
   {
   		var height = 0 , newHeight = 0;
		height = Core.Capabilities.getScreenHeight();
		newHeight = (height / 480) * y;
      
       return newHeight;
   },
    
	adjustWidthWithDevice: function(aWidth)
	{
		var w = 0, calWidth = 0;
		
		w = Core.Capabilities.getScreenWidth();
		
		calWidth = aWidth * (w/320);
		//////NgLogE('Bandri Width:'+aWidth +'-->'+calWidth +'device W:'+w);
		return calWidth;			
	},
	adjustHeightWithDevice: function(aHeight)
	{
		var h = 0 , calHeight = 0;
		
		h = Core.Capabilities.getScreenHeight();
		
		calHeight = aHeight * (h/480);
		//////NgLogE('Bandri Width:'+aHeight +'-->'+calHeight +'device H:'+h);
		return calHeight;		
		
	},
	
	getAdjustedFontSize: function(fieldHeight) {
        return this.getAdjustedX(fieldHeight / 2) / aspectRatioFactor.d;
    },
	////////////////////////////////////////////////////////////
	
	destroy: function($super)
	{
		// removing from NGWindow
		//NGWindow.document.removeChild(this);
		
		// removing childs from this view and destroying them
		
		if(this._titleLabel)
		{
			this._titleLabel.destroy();
			this._titleLabel=null;
		}
		
		if(this._cancelButton)
		{
			this._cancelButton.destroy();
			this._cancelButton=null;
		}
		
		if(this._backgroundImage)
		{
			this._backgroundImage.destroy();
			this._backgroundImage=null;
		}
		
		if( this._okButton)
		{	
			this._okButton.destroy();
			this._okButton=null;
		}
		
		if (this.editText)
		{
			this.editText.destroy();
			this.editText = null;
		}
		$super();
	},
});